package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.DeleteDishException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
	//mapper层数据接口
	@Autowired
	private SetmealMapper setmealMapper;
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private SetmealDishMapper setmealDishMapper;
	
	@Autowired
	private DishMapper dishMapper;
	
	//根据id删除
	@Override
	public void deleteByIds(Long[] ids) {
		//不可删除在售的套餐
		//删除前查询语句,查询ids中状态为1 的套餐
		//select count(0) from setmeal where status = 1 and id in #{ids}
		LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Setmeal::getStatus,1)
				.in(Setmeal::getId,ids);
		
		Integer count = setmealMapper.selectCount(wrapper);
		if (count > 0) {
			throw new DeleteDishException("当前套餐为在售状态,不可删除");
		}
		
		//如果套餐未有在售状态
		//delete from setmeal where status = 0 and id in #{ids}
		LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
		
		//删除中间表
		wrapper1.in(SetmealDish::getSetmealId, ids);
		setmealDishMapper.delete(wrapper1);
		
		//删除套餐
		LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
		wrapper2.eq(Setmeal::getStatus,0)
				.in(Setmeal::getId,ids);
		setmealMapper.delete(wrapper2);
		
		
	}
	
	//保存(新增)套餐
	@Override
	public void save(Setmeal setmeal) {
		setmealMapper.insert(setmeal);
		
		//遍历前端传入的菜品信息
		List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
		
		if ((CollectionUtil.isNotEmpty(setmealDishList))) {
			for (SetmealDish setmealDish : setmealDishList) {
				//设置套餐Id
				setmealDish.setSetmealId(setmeal.getId());
				
				//将菜品保存到setmeal_dish,套餐包含的菜品表中
				setmealDishMapper.insert(setmealDish);
			}
		}
	}
	
	@Override
	public List<SetmealDish> findSetmealDish(String id) {
		//从setmeal中查找dish信息
		LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SetmealDish::getSetmealId,id);
		List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
		
		if (CollectionUtil.isNotEmpty(setmealDishList)) {
			for (SetmealDish setmealDish : setmealDishList) {
				//从dish表中查图片保存到setmealDish中
				LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
				wrapper1.eq(Dish::getId,setmealDish.getDishId());
				Dish dish = dishMapper.selectOne(wrapper1);
				
				setmealDish.setImage(dish.getImage());
			}
		}
		
		return setmealDishList;
	}
	
	@Override
	public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {
		//查询套餐中的信息
		LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
		
		wrapper.eq(Setmeal::getCategoryId,categoryId)
				.eq(Setmeal::getStatus,status);
		
		return setmealMapper.selectList(wrapper);
	}
	
	@Override
	public void batchStopAndStart(Integer status, List<String> ids) {
		if (ids != null && ids.size() > 0) {
			for (String id : ids) {
				//更新方法
				UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
				wrapper.set("status",status)
						.eq("id",id);
				setmealMapper.update(null,wrapper);
			}
		}
	
	}
	
	@Override
	public void update(Setmeal setmeal) {
		//更新步骤
		//更新套餐表
		setmealMapper.updateById(setmeal);
		//删除套餐对应的setmealDish表中信息
		LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
		setmealDishMapper.delete(wrapper);
		//添加setmealDish
		List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
		
		if (setmealDishes != null && setmealDishes.size() > 0) {
			for (SetmealDish setmealDish : setmealDishes) {
				setmealDish.setSetmealId(setmeal.getId());
				setmealDishMapper.insert(setmealDish);
			}
		}
	}
	
	@Override
	public Setmeal findById(String id) {
		//从套餐表中查询套餐信息
		Setmeal setmeal = setmealMapper.selectById(id);
		//根据套餐信息从套餐菜品表中查菜品
		LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
		List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
		setmeal.setSetmealDishes(setmealDishes);
		return setmeal;
	}
	
	@Override
	public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
		//根据name模糊分页查询套餐信息(setmeal表中)
		//设置分页数据
		Page<Setmeal> page = new Page<>(pageNum, pageSize);
		
		//封装查询条件
		LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
		
		//分页查询,分页查询设置分页对象和查询条件
		page = setmealMapper.selectPage(page, wrapper);
		
		//遍历
		List<Setmeal> records = page.getRecords();
		if (CollectionUtil.isNotEmpty(records)) {
			//根据套餐中的categoryId去category表中查询对应的信息
			for (Setmeal record : records) {
				
				Category category = categoryMapper.selectById(record.getCategoryId());
				record.setCategoryName(category.getName());
			}
		}
		return page;
	}
}
